home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / dev / misc / AmigaSDLsrc.lha / amisrc / SDL_sysjoystick.c < prev    next >
C/C++ Source or Header  |  2001-05-05  |  6KB  |  241 lines

  1. /*
  2.     SDL - Simple DirectMedia Layer
  3.     Copyright (C) 1997, 1998, 1999, 2000  Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     slouken@devolution.com
  21. */
  22.  
  23.  
  24. #ifdef SAVE_RCSID
  25. static char rcsid =
  26.  "@(#) $Id: SDL_sysjoystick.c,v 1.1.2.4 2000/07/15 23:54:11 hercules Exp $";
  27. #endif
  28.  
  29. /* This is the system specific header for the SDL joystick API */
  30.  
  31. #include <stdio.h>        /* For the definition of NULL */
  32.  
  33. #include <libraries/lowlevel.h>
  34. #ifdef __SASC
  35. #include <proto/exec.h>
  36. #include <proto/lowlevel.h>
  37. #include <proto/graphics.h>
  38. #else
  39. #include <inline/exec.h>
  40. #include <inline/lowlevel.h>
  41. #include <inline/graphics.h>
  42. #endif
  43. #include "mydebug.h"
  44.  
  45. extern struct ExecBase *SysBase;
  46. extern struct GfxBase *GfxBase;
  47. #include <stdlib.h>
  48.  
  49. #include "SDL_error.h"
  50. #include "SDL_joystick.h"
  51. #include "SDL_sysjoystick.h"
  52. #include "SDL_joystick_c.h"
  53.  
  54. /* Function to scan the system for joysticks.
  55.  * This function should set SDL_numjoysticks to the number of available
  56.  * joysticks.  Joystick 0 should be the system default joystick.
  57.  * It should return 0, or -1 on an unrecoverable fatal error.
  58.  */
  59.  
  60.  
  61. /* Amiga specific datas */
  62. struct Library *LowLevelBase=NULL;
  63.  
  64. ULONG joybut[]=
  65. {
  66.     JPF_BUTTON_RED,
  67.     JPF_BUTTON_BLUE,
  68.     JPF_BUTTON_PLAY,
  69.     JPF_BUTTON_YELLOW,
  70.     JPF_BUTTON_GREEN,
  71.     JPF_BUTTON_FORWARD,
  72.     JPF_BUTTON_REVERSE,
  73. };
  74.  
  75. struct joystick_hwdata 
  76. {
  77.     ULONG joystate;
  78. };
  79.  
  80. int SDL_SYS_JoystickInit(void)
  81. {
  82.     if(!LowLevelBase)
  83.     {
  84.         if(LowLevelBase=OpenLibrary("lowlevel.library",37))
  85.             return 2;
  86.     }
  87.     else
  88.         return 2;
  89.  
  90.     D(bug("%ld joysticks available.\n",SDL_numjoysticks));
  91.  
  92.     return 0;
  93. }
  94.  
  95. /* Function to get the device-dependent name of a joystick */
  96. const char *SDL_SYS_JoystickName(int index)
  97. {
  98.     if(index<2&&LowLevelBase)
  99.     {
  100.         switch(index)
  101.         {
  102.             case 0:
  103.                 return "Port 1 Amiga Joystick/Joypad";
  104.             case 1:
  105.                 return "Port 2 Amiga Joystick/Joypad";
  106.         }
  107.     }
  108.  
  109.     SDL_SetError("No joystick available with that index");
  110.     return(NULL);
  111. }
  112.  
  113. /* Function to open a joystick for use.
  114.    The joystick to open is specified by the index field of the joystick.
  115.    This should fill the nbuttons and naxes fields of the joystick structure.
  116.    It returns 0, or -1 if there is an error.
  117.  */
  118.  
  119. int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
  120. {
  121.     ULONG temp,i;
  122.     D(bug("Opening joystick %ld\n",joystick->index));
  123.  
  124.     if(!(joystick->hwdata=malloc(sizeof(struct joystick_hwdata))))
  125.         return -1;
  126.  
  127. /* This loop is to check if the controller is a joypad */
  128.  
  129.     for(i=0;i<20;i++)
  130.     {
  131.         temp=ReadJoyPort(joystick->index);
  132.         WaitTOF();
  133.     }
  134.  
  135.     if((temp&JP_TYPE_MASK)==JP_TYPE_GAMECTLR)
  136.         joystick->nbuttons=7;
  137.     else
  138.         joystick->nbuttons=3;
  139.  
  140.     joystick->nhats=0;
  141.     joystick->nballs=0;
  142.     joystick->naxes=2;
  143.     joystick->hwdata->joystate=0L;
  144.  
  145.     return 0;
  146. }
  147.  
  148. /* Function to update the state of a joystick - called as a device poll.
  149.  * This function shouldn't update the joystick structure directly,
  150.  * but instead should call SDL_PrivateJoystick*() to deliver events
  151.  * and update joystick device state.
  152.  */
  153. void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
  154. {
  155.     ULONG data;    
  156.     int i;
  157.  
  158.     if(joystick->index<2)
  159.     {
  160.         data=ReadJoyPort(joystick->index);
  161.  
  162.         if(data&JP_DIRECTION_MASK)
  163.         {
  164.             if(data&JPF_JOY_DOWN)
  165.             {
  166.                 if(!(joystick->hwdata->joystate&JPF_JOY_DOWN))
  167.                     SDL_PrivateJoystickAxis(joystick,0,127);
  168.             }
  169.             else if(data&JPF_JOY_UP)
  170.             {
  171.                 if(!(joystick->hwdata->joystate&JPF_JOY_UP))
  172.                     SDL_PrivateJoystickAxis(joystick,0,-127);
  173.             }
  174.             else if(joystick->hwdata->joystate&(JPF_JOY_UP|JPF_JOY_DOWN))
  175.                 SDL_PrivateJoystickAxis(joystick,0,0);
  176.  
  177.             if(data&JPF_JOY_LEFT)
  178.             {
  179.                 if(!(joystick->hwdata->joystate&JPF_JOY_LEFT))
  180.                     SDL_PrivateJoystickAxis(joystick,1,-127);
  181.             }
  182.             else if(data&JPF_JOY_RIGHT)
  183.             {
  184.                 if(!(joystick->hwdata->joystate&JPF_JOY_RIGHT))
  185.                     SDL_PrivateJoystickAxis(joystick,1,127);
  186.             }
  187.             else if(joystick->hwdata->joystate&(JPF_JOY_LEFT|JPF_JOY_RIGHT))
  188.                 SDL_PrivateJoystickAxis(joystick,1,0);
  189.         }
  190.         else if(joystick->hwdata->joystate&(JPF_JOY_LEFT|JPF_JOY_RIGHT))
  191.         {
  192.                 SDL_PrivateJoystickAxis(joystick,1,0);
  193.         }
  194.         else if(joystick->hwdata->joystate&(JPF_JOY_UP|JPF_JOY_DOWN))
  195.         {
  196.                 SDL_PrivateJoystickAxis(joystick,0,0);
  197.         }
  198.  
  199.         for(i=0;i<joystick->nbuttons;i++)
  200.         {
  201.             if( (data&joybut[i]) )
  202.             {
  203.                 if(i==1)
  204.                     data&=(~(joybut[2]));
  205.  
  206.                 if(!(joystick->hwdata->joystate&joybut[i]))
  207.                     SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
  208.             }
  209.             else if(joystick->hwdata->joystate&joybut[i])
  210.                 SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
  211.         }
  212.  
  213.         joystick->hwdata->joystate=data;
  214.     }
  215.  
  216.     return;
  217. }
  218.  
  219. /* Function to close a joystick after use */
  220. void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
  221. {
  222.     if(joystick->hwdata)
  223.         free(joystick->hwdata);
  224.     return;
  225. }
  226.  
  227. /* Function to perform any system-specific joystick related cleanup */
  228.  
  229. void SDL_SYS_JoystickQuit(void)
  230. {
  231.     if(LowLevelBase)
  232.     {
  233.         CloseLibrary(LowLevelBase);
  234.         LowLevelBase=NULL;
  235.         SDL_numjoysticks=0;
  236.     }
  237.  
  238.     return;
  239. }
  240.  
  241.